<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="taskList">
      <!--      <el-table-column label="流程ID" align="center" prop="id"/>-->
      <el-table-column label="流程名称" align="center" prop="instanceName" />
      <el-table-column label="任务节点名称" align="center" prop="name" />
      <el-table-column label="任务状态" align="center" prop="status" />
      <el-table-column label="办理人" align="center" prop="assignee" />
      <el-table-column label="创建时间" align="center" prop="createdDate" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="examineAndApprove(scope.row)"
            v-hasPermi="['workflow:leave:edit']"
            >审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form
        ref="buyBackOrderForm"
        :model="form"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :disabled="formDisabled"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="采购退货编码" prop="buyBackOrderCode">
              <el-input
                v-model="form.buyBackOrderCode"
                placeholder="请输入采购退货编码"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购退货申请单" prop="buyBackReqId">
              <el-select
                :value="form.buyBackReqId"
                clearable
                placeholder="请选择采购退货申请单"
              >
                <el-option
                  :label="form.buyBackReq.code"
                  :value="form.buyBackReqId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商" prop="supplierId">
              <el-input :value="form.supName" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据日期" prop="orderDate">
              <el-date-picker
                clearable
                v-model="form.orderDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择单据日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="源单业务员">
              <el-input
                :value="form.srcUser.userName"
                placeholder="请选择采购退货申请单"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务部门">
              <el-input
                :value="form.srcDept.deptName"
                placeholder="请选择采购退货申请单"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="应出库数量" prop="allOutStock">
              <el-input :value="form.allOutStock" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="实际出库数量">
              <el-input :value="form.realOutStock" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="应收款">
              <el-input :value="form.allInPrice" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="实际收款" prop="realInPrice">
              <el-input :value="form.realInPrice" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库状态">
              <el-input :value="form.stockStatusName" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收款状态">
              <el-input :value="form.payStatusName" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="创建人" prop="createBy">
              <el-input :value="form.createBy" placeholder="" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建时间" prop="createDate">
              <el-date-picker
                clearable
                :value="form.createDate"
                type="date"
                value-format="yyyy-MM-dd"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="修改人">
              <el-input :value="form.modifyBy" placeholder="" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="修改时间" prop="modifyDate">
              <el-date-picker
                clearable
                :value="form.modifyDate"
                type="date"
                value-format="yyyy-MM-dd"
                disabled
                placeholder=""
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生效时间" prop="effectDate">
              <el-date-picker
                clearable
                :value="form.effectDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder=""
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-menu default-active="2" class="el-menu-demo" mode="horizontal">
          <el-menu-item index="2">采购退货明细</el-menu-item>
        </el-menu>
        <el-table
          ref="orderTable"
          :data="form.buyBackOrderDetailList"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column label="序号" width="70" align="center">
            <template slot-scope="scope">
              {{
                scope.$index +
                1 +
                (queryParams.pageNum - 1) * queryParams.pageSize
              }}
            </template>
          </el-table-column>
          <el-table-column prop="goods.proName" label="商品" width="150">
          </el-table-column>
          <el-table-column prop="houseName" label="仓库" width="120">
          </el-table-column>
          <el-table-column prop="goods.unitName" label="单位" width="120">
          </el-table-column>
          <el-table-column prop="outStock" label="退货数量" width="80">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.outStock"
                :min="0"
                @blur="validateStock(scope.row)"
                @input="calculateCostAll(scope.row)"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="cost" label="单价" width="120">
          </el-table-column>
          <el-table-column prop="inPrice" label="结算金额" width="120">
            <template slot-scope="scope">
              <el-input :value="scope.row.inPrice" readonly></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="quantity" label="申请退货数量" width="120">
          </el-table-column>
          <el-table-column prop="stock" label="库存数量" width="120">
          </el-table-column>
        </el-table>
      </el-form>
      <el-form
        :model="approval"
        ref="form"
        label-width="100px"
        class="demo-dynamic"
      >
        <el-form-item label="审批结果">
          <el-radio-group v-model="approval.approvalResult">
            <el-radio :label="2">同意</el-radio>
            <el-radio :label="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见">
          <el-input
            type="textarea"
            v-model="approval.approvalOption"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTask,
  handleApproval,
  buyBackOrderShow,
} from "@/api/activiti/task";

export default {
  name: "Leave",
  data() {
    return {
      id: "",
      definitionKey: "",
      businessKey: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假表格数据
      taskList: [],
      buyBackReq: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单是否禁用
      formDisabled: true,
      // 查询参数
      queryParams: {
        processDefinitionKey: "buyBackOrderBPMN",
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        buyBackReq: { code: "" },
        // 内部选择采购退货申请单
        srcUser: { userName: "" },
        srcDept: { srcDeptName: "" },
        //采购退货订单详情列表
        buyBackOrderDetailList: [],
        approvalUser: { userName: "" },
      },
      approval: {
        approvalResult: 2,
        approvalOption: "",
      },
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购退货申请列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then((response) => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      (this.definitionKey = ""),
        (this.businessKey = ""),
        (this.form = {
          buyBackReq: { code: "" },
          // 内部选择采购退货申请单
          srcUser: { userName: "" },
          srcDept: { srcDeptName: "" },
          //采购退货订单详情列表
          buyBackOrderDetailList: [],
          approvalUser: { userName: "" },
        });
      this.resetForm("form");
    },

    /** 审批按钮操作 */
    examineAndApprove(row) {
      this.reset();
      this.definitionKey = row.definitionKey;
      this.businessKey = row.businessKey;
      this.id = row.id;
      buyBackOrderShow(this.id).then((rs) => {
        this.form = rs.data;
        console.log(this.form);
      });
      this.open = true;
      this.title = "审批采购退货单";
    },
    /** 提交按钮 */
    submitForm() {
      this.approval.taskId = this.id;
      handleApproval(this.approval).then((response) => {
        this.$modal.msgSuccess("审批成功");
        this.open = false;
        this.getList();
      });
    },
    /**文档预览**/
    preView(filePath) {
      console.info(filePath);
      if (null == filePath || "" == filePath) {
        this.$alert("申请人没有上传哦", "消息提示", {
          confirmButtonText: "确定",
        });
      } else {
        // let originUrl = 'http://127.0.0.1:80/ruoyi-admin/';   //要预览文件的访问地址'
        window.open(filePath);
      }
      // const bb = originUrl  + aa;
      // window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(bb)));
    },
  },
};
</script>
